//
//  RSIDAuth.m
//  StaffTax
//
//  Created by 侯伟 on 2020/4/3.
//  Copyright © 2020 侯伟. All rights reserved.
//

#import "RSIDAuth.h"
#import <LocalAuthentication/LocalAuthentication.h>
@implementation RSIDAuth

/**
 * 启动TouchID/FaceID进行验证
 * @param describe TouchID/FaceID显示的描述
 * @param complete 回调状态的complete
 */
+ (void)authIDWithDescribe:(NSString *)describe complete:(RSIDAuthStateBlock)complete{
    if(!describe.length) {
        if(isIphoneX){
            describe = @"验证已有面容";
        }else{
            describe = @"通过Home键验证已有指纹";
        }
    }
    
    if (NSFoundationVersionNumber < NSFoundationVersionNumber_iOS_8_0) {
        [Queue asyncMain:^{
            NSLog(@"系统版本不支持TouchID/FaceID (必须高于iOS 8.0才能使用)");
            complete(RSIDAuthStateVersionNotSupport, NULL);
        }];
        return;
    }
    LAContext *context = [[LAContext alloc] init];
    // 认证失败提示信息，为 @"" 则不提示
    context.localizedFallbackTitle = @"输入密码";
    NSError *error = nil;
    // LAPolicyDeviceOwnerAuthenticationWithBiometrics: 用TouchID/FaceID验证
    // LAPolicyDeviceOwnerAuthentication: 用TouchID/FaceID或密码验证, 默认是错误两次或锁定后, 弹出输入密码界面（本案例使用）
    if ([context canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&error]) {
        [context evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:describe reply:^(BOOL success, NSError * _Nullable error) {
            if (success) {
                [Queue asyncMain:^{
                    NSLog(@"TouchID/FaceID 验证成功");
                    complete(RSIDAuthStateSuccess, error);
                }];
            }else if(error){
                if (@available(iOS 11.0, *)) {
                    switch (error.code) {
                        case LAErrorAuthenticationFailed:{
                            [Queue asyncMain:^{
                                NSLog(@"TouchID/FaceID 验证失败");
                                complete(RSIDAuthStateFail, error);
                            }];
                            break;
                        }
                        case LAErrorUserCancel:{
                            [Queue asyncMain:^{
                                NSLog(@"TouchID/FaceID 被用户手动取消");
                                complete(RSIDAuthStateUserCancel, error);
                            }];
                        }
                            break;
                        case LAErrorUserFallback:{
                            [Queue asyncMain:^{
                                NSLog(@"用户不使用TouchID/FaceID,选择手动输入密码");
                                complete(RSIDAuthStateInputPassword, error);
                            }];
                        }
                            break;
                        case LAErrorSystemCancel:{
                            [Queue asyncMain:^{
                                NSLog(@"TouchID/FaceID 被系统取消 (如遇到来电,锁屏,按了Home键等)");
                                complete(RSIDAuthStateSystemCancel, error);
                            }];
                        }
                            break;
                        case LAErrorPasscodeNotSet:{
                            [Queue asyncMain:^{
                                NSLog(@"TouchID/FaceID 无法启动,因为用户没有设置密码");
                                complete(RSIDAuthStatePasswordNotSet, error);
                            }];
                        }
                            break;
                        case LAErrorBiometryNotEnrolled:{
                            [Queue asyncMain:^{
                                NSLog(@"TouchID/FaceID 无法启动,因为用户没有设置TouchID/FaceID");
                                complete(RSIDAuthStateTouchIDNotSet, error);
                            }];
                        }
                            break;
                        case LAErrorBiometryNotAvailable:{
                            [Queue asyncMain:^{
                                NSLog(@"TouchID/FaceID 无效");
                                complete(RSIDAuthStateTouchIDNotAvailable, error);
                            }];
                        }
                            break;
                        case LAErrorBiometryLockout:{
                            [Queue asyncMain:^{
                                NSLog(@"TouchID/FaceID 被锁定(连续多次验证TouchID/FaceID失败,系统需要用户手动输入密码)");
                                complete(RSIDAuthStateTouchIDLockout, error);
                            }];
                        }
                            break;
                        case LAErrorAppCancel:{
                            [Queue asyncMain:^{
                                NSLog(@"当前软件被挂起并取消了授权 (如App进入了后台等)");
                                complete(RSIDAuthStateAppCancel, error);
                            }];
                        }
                            break;
                        case LAErrorInvalidContext:{
                            [Queue asyncMain:^{
                                NSLog(@"当前软件被挂起并取消了授权 (LAContext对象无效)");
                                complete(RSIDAuthStateInvalidContext, error);
                            }];
                        }
                            break;
                        default:
                            break;
                    }
                } else {
                    // iOS 11.0以下的版本只有 TouchID 认证
                    switch (error.code) {
                        case LAErrorAuthenticationFailed:{
                            [Queue asyncMain:^{
                                NSLog(@"TouchID 验证失败");
                                complete(RSIDAuthStateFail, error);
                            }];
                            break;
                        }
                        case LAErrorUserCancel:{
                            [Queue asyncMain:^{
                                NSLog(@"TouchID 被用户手动取消");
                                complete(RSIDAuthStateUserCancel, error);
                            }];
                        }
                            break;
                        case LAErrorUserFallback:{
                            [Queue asyncMain:^{
                                NSLog(@"用户不使用TouchID,选择手动输入密码");
                                complete(RSIDAuthStateInputPassword, error);
                            }];
                        }
                            break;
                        case LAErrorSystemCancel:{
                            [Queue asyncMain:^{
                                NSLog(@"TouchID 被系统取消 (如遇到来电,锁屏,按了Home键等)");
                                complete(RSIDAuthStateSystemCancel, error);
                            }];
                        }
                            break;
                        case LAErrorPasscodeNotSet:{
                            [Queue asyncMain:^{
                                NSLog(@"TouchID 无法启动,因为用户没有设置密码");
                                complete(RSIDAuthStatePasswordNotSet, error);
                            }];
                        }
                            break;
                        case LAErrorTouchIDNotEnrolled:{
                            [Queue asyncMain:^{
                                NSLog(@"TouchID 无法启动,因为用户没有设置TouchID");
                                complete(RSIDAuthStateTouchIDNotSet, error);
                            }];
                        }
                            break;
                            //case :{
                        case LAErrorTouchIDNotAvailable:{
                            [Queue asyncMain:^{
                                NSLog(@"TouchID 无效");
                                complete(RSIDAuthStateTouchIDNotAvailable, error);
                            }];
                        }
                            break;
                        case LAErrorTouchIDLockout:{
                            [Queue asyncMain:^{
                                NSLog(@"TouchID 被锁定(连续多次验证TouchID失败,系统需要用户手动输入密码)");
                                complete(RSIDAuthStateTouchIDLockout, error);
                            }];
                        }
                            break;
                        case LAErrorAppCancel:{
                            [Queue asyncMain:^{
                                NSLog(@"当前软件被挂起并取消了授权 (如App进入了后台等)");
                                complete(RSIDAuthStateAppCancel, error);
                            }];
                        }
                            break;
                        case LAErrorInvalidContext:{
                            [Queue asyncMain:^{
                                NSLog(@"当前软件被挂起并取消了授权 (LAContext对象无效)");
                                complete(RSIDAuthStateInvalidContext, error);
                            }];
                        }
                            break;
                        default:
                            break;
                    }
                }
            }
        }];
        
    }else{
        [Queue asyncMain:^{
            NSLog(@"当前设备不支持TouchID/FaceID");
            complete(RSIDAuthStateNotSupport, error);
        }];
    }
}
@end
